import { _decorator, Component, Node } from 'cc';
import { EnemyState } from './EnemyState';
import { EnemyStateMachine } from './EnemyStateMachine';
import { Enemy } from './Enemy';
import { Entity } from '../Entity';
import { Player } from '../Player/Player';
const { ccclass, property } = _decorator;

@ccclass('EnemyAttackState')
export class EnemyAttackState extends EnemyState {
    private attackDamage: number = 10; // 伤害值


    constructor(_enemy: Enemy, _stateMachine: EnemyStateMachine) {
        super(_enemy, _stateMachine);
    }

    override enter(): void {
        super.enter();
        this.enemy.playAnimation("gongji", false);
        this.attackPlayer();
        this.enemy.skeleton.setCompleteListener((trackEntry) => {
            this.stateMachine.changeState(this.enemy.battleState);
        });

    }

    override update(dt: number): void {
        super.update(dt);
        this.enemy.SetVelocity(0, 0); // 停止移动

    }

    override exit(): void {
        super.exit();
        this.enemy.lastAttackTime = Date.now() / 1000;
    }



    // 执行攻击逻辑
    private attackPlayer() {
        if (this.enemy.player) {
            this.enemy.audioSource.playOneShot(this.enemy.attackClip, 0.5)
            this.enemy.player.getComponent(Player).TakeDamage(this.attackDamage);

        } else {
            this.stateMachine.changeState(this.enemy.idleState);
        }
    }
}
